import { ref } from 'vue';
import api from 'axios';
import { useMessageNotify } from 'src/hook/common/useMessageNotify';

//查询参数
export interface QueryParam {
  id: number
}

//查询条件类型
export type QryParamType = QueryParam | null;

//接收到数据,id和子节点id有重复
export interface uTreeItem {
  id: number,
  label: string,
  children: uTreeItem[]
}

//转传成sid唯一性
export interface uTreeItemVO {
  sid: string,
  name: string,
  children: uTreeItemVO[]
}

//将sid转换成父节点id和子节点id
export interface uTreeIndex {
  parentId: number,
  sonId: number
}

export function useCommonTree () {
  //消息通知
  const {
    showMessage
  } = useMessageNotify();

  //存储数据数组
  let arrList = [] as uTreeItem[];
  let uTreeList = ref<uTreeItemVO[]>([]);


  //结构转换
  const convertToTreeVOMap = (arr: uTreeItem[]) => {
    let listArr = [] as uTreeItemVO[];

    //数组遍历
    arr.forEach((item: uTreeItem) => {
      let child = [] as uTreeItemVO[];

      //子节点遍历，id为父节点和子节点合成
      if (item.children && item.children.length > 0) {
        item.children.forEach((el) => {
          child.push({
            sid: item.id.toString() + '-' + el.id.toString(),
            name: el.label,
            children: []
          })
        })
      }

      //父节点
      listArr.push({
        sid: item.id.toString(),
        name: item.label,
        children: child
      });
    });

    return listArr;
  }

  //读取页面树
  const getCurrentTree = (url: string, param: QryParamType) => {
    if (param) {
      api.post(url, { id: param.id }).then((res: any) => {
        if (res.data.message === 'Success') {
          arrList.length = 0;
          arrList = res.data.data;
          //console.log(arrList);
          uTreeList.value = convertToTreeVOMap(arrList);
        } else {
          showMessage(res.data.message);
        }
      }).catch((error) => {
        showMessage(error.message);
      });
    } else {
      api.post(url).then((res: any) => {
        if (res.data.message === 'Success') {
          arrList.length = 0;
          arrList = res.data.data;
          //console.log(arrList);
          uTreeList.value = convertToTreeVOMap(arrList);
        } else {
          showMessage(res.data.message);
        }
      }).catch((error) => {
        showMessage(error.message);
      });
    }
  }

  //从sid中取出id
  const convertToTreeItem = (sid: string): uTreeIndex | null => {
    if (sid === null) {
      return null;
    }

    let index = sid.indexOf('-');
    if (sid.length <= 0) {
      return null;
    }

    //只是父节点
    if (index === -1) {
      let ps = sid.substring(0, sid.length);
      let ps_id = parseInt(ps);
      if (isNaN(ps_id)) {
        return null;
      } else {
        return {
          parentId: ps_id,
          sonId: 0
        }
      }
    } else {
      let ps = sid.substring(0, index);
      let cs = sid.substring(index + 1);
      let ps_id = parseInt(ps);
      let cs_id = parseInt(cs);
      if ((isNaN(ps_id)) || (isNaN(cs_id))) {
        return null;
      } else {
        return {
          parentId: ps_id,
          sonId: cs_id
        }
      }
    }
  }

  return {
    uTreeList,
    getCurrentTree,
    convertToTreeItem
  }
}
